1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.custom;
15 
16 
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.widgets.*;
21 
22 /**
23 *
24 * A TreeEditor is a manager for a Control that appears above a cell in a Tree and tracks with the
25 * moving and resizing of that cell.  It can be used to display a text widget above a cell
26 * in a Tree so that the user can edit the contents of that cell.  It can also be used to display
27 * a button that can launch a dialog for modifying the contents of the associated cell.
28 *
29 * <p> Here is an example of using a TreeEditor:</p>
30 * <pre><code>
31 *	final Tree tree = new Tree(shell, SWT.BORDER);
32 *	for (int i = 0; i &lt; 3; i++) {
33 *		TreeItem item = new TreeItem(tree, SWT.NONE);
34 *		item.setText("item " + i);
35 *		for (int j = 0; j &lt; 3; j++) {
36 *			TreeItem subItem = new TreeItem(item, SWT.NONE);
37 *			subItem.setText("item " + i + " " + j);
38 *		}
39 *	}
40 *
41 *	final TreeEditor editor = new TreeEditor(tree);
42 *	//The editor must have the same size as the cell and must
43 *	//not be any smaller than 50 pixels.
44 *	editor.horizontalAlignment = SWT.LEFT;
45 *	editor.grabHorizontal = true;
46 *	editor.minimumWidth = 50;
47 *
48 *	tree.addSelectionListener(new SelectionAdapter() {
49 *		public void widgetSelected(SelectionEvent e) {
50 *			// Clean up any previous editor control
51 *			Control oldEditor = editor.getEditor();
52 *			if (oldEditor != null) oldEditor.dispose();
53 *
54 *			// Identify the selected row
55 *			TreeItem item = (TreeItem)e.item;
56 *			if (item == null) return;
57 *
58 *			// The control that will be the editor must be a child of the Tree
59 *			Text newEditor = new Text(tree, SWT.NONE);
60 *			newEditor.setText(item.getText());
61 *			newEditor.addModifyListener(new ModifyListener() {
62 *				public void modifyText(ModifyEvent e) {
63 *					Text text = (Text)editor.getEditor();
64 *					editor.getItem().setText(text.getText());
65 *				}
66 *			});
67 *			newEditor.selectAll();
68 *			newEditor.setFocus();
69 *			editor.setEditor(newEditor, item);
70 *		}
71 *	});
72 * </code></pre>
73 *
74 * @see <a href="http://www.eclipse.org/swt/snippets/#treeeditor">TreeEditor snippets</a>
75 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
76 */
77 public class TreeEditor extends ControlEditor {
78 	Tree tree;
79 	TreeItem item;
80 	int column = 0;
81 	ControlListener columnListener;
82 	TreeListener treeListener;
83 	Runnable timer;
84 	static final int TIMEOUT = 1500;
85 
86 /**
87 * Creates a TreeEditor for the specified Tree.
88 *
89 * @param tree the Tree Control above which this editor will be displayed
90 *
91 */
TreeEditor(Tree tree)92 public TreeEditor (Tree tree) {
93 	super(tree);
94 	this.tree = tree;
95 
96 	columnListener = new ControlListener() {
97 		@Override
98 		public void controlMoved(ControlEvent e){
99 			layout();
100 		}
101 		@Override
102 		public void controlResized(ControlEvent e){
103 			layout();
104 		}
105 	};
106 	timer = () -> layout ();
107 	treeListener = new TreeListener () {
108 		final Runnable runnable = () -> {
109 			if (editor == null || editor.isDisposed()) return;
110 			if (TreeEditor.this.tree.isDisposed()) return;
111 			layout();
112 			editor.setVisible(true);
113 		};
114 		@Override
115 		public void treeCollapsed(TreeEvent e) {
116 			if (editor == null || editor.isDisposed ()) return;
117 			editor.setVisible(false);
118 			e.display.asyncExec(runnable);
119 		}
120 		@Override
121 		public void treeExpanded(TreeEvent e) {
122 			if (editor == null || editor.isDisposed ()) return;
123 			editor.setVisible(false);
124 			e.display.asyncExec(runnable);
125 		}
126 	};
127 	tree.addTreeListener(treeListener);
128 
129 	// To be consistent with older versions of SWT, grabVertical defaults to true
130 	grabVertical = true;
131 }
132 
133 @Override
computeBounds()134 Rectangle computeBounds () {
135 	if (item == null || column == -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
136 	Rectangle cell = item.getBounds(column);
137 	Rectangle rect = item.getImageBounds(column);
138 	cell.x = rect.x + rect.width;
139 	cell.width -= rect.width;
140 	Rectangle area = tree.getClientArea();
141 	if (cell.x < area.x + area.width) {
142 		if (cell.x + cell.width > area.x + area.width) {
143 			cell.width = area.x + area.width - cell.x;
144 		}
145 	}
146 	Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
147 
148 	if (grabHorizontal) {
149 		if (tree.getColumnCount() == 0) {
150 			// Bounds of tree item only include the text area - stretch out to include
151 			// entire client area
152 			cell.width = area.x + area.width - cell.x;
153 		}
154 		editorRect.width = Math.max(cell.width, minimumWidth);
155 	}
156 
157 	if (grabVertical) {
158 		editorRect.height = Math.max(cell.height, minimumHeight);
159 	}
160 
161 	if (horizontalAlignment == SWT.RIGHT) {
162 		editorRect.x += cell.width - editorRect.width;
163 	} else if (horizontalAlignment == SWT.LEFT) {
164 		// do nothing - cell.x is the right answer
165 	} else { // default is CENTER
166 		editorRect.x += (cell.width - editorRect.width)/2;
167 	}
168 	// don't let the editor overlap with the +/- of the tree
169 	editorRect.x = Math.max(cell.x, editorRect.x);
170 
171 	if (verticalAlignment == SWT.BOTTOM) {
172 		editorRect.y += cell.height - editorRect.height;
173 	} else if (verticalAlignment == SWT.TOP) {
174 		// do nothing - cell.y is the right answer
175 	} else { // default is CENTER
176 		editorRect.y += (cell.height - editorRect.height)/2;
177 	}
178 	return editorRect;
179 }
180 
181 /**
182  * Removes all associations between the TreeEditor and the row in the tree.  The
183  * tree and the editor Control are <b>not</b> disposed.
184  */
185 @Override
dispose()186 public void dispose () {
187 	if (tree != null && !tree.isDisposed()) {
188 		if (this.column > -1 && this.column < tree.getColumnCount()){
189 			TreeColumn treeColumn = tree.getColumn(this.column);
190 			treeColumn.removeControlListener(columnListener);
191 		}
192 		if (treeListener != null) tree.removeTreeListener(treeListener);
193 	}
194 	columnListener = null;
195 	treeListener = null;
196 	tree = null;
197 	item = null;
198 	column = 0;
199 	timer = null;
200 	super.dispose();
201 }
202 
203 /**
204 * Returns the zero based index of the column of the cell being tracked by this editor.
205 *
206 * @return the zero based index of the column of the cell being tracked by this editor
207 *
208 * @since 3.1
209 */
getColumn()210 public int getColumn () {
211 	return column;
212 }
213 
214 /**
215 * Returns the TreeItem for the row of the cell being tracked by this editor.
216 *
217 * @return the TreeItem for the row of the cell being tracked by this editor
218 */
getItem()219 public TreeItem getItem () {
220 	return item;
221 }
222 
resize()223 void resize () {
224 	layout();
225 	/*
226 	 * On some platforms, the table scrolls when an item that
227 	 * is partially visible at the bottom of the table is
228 	 * selected.  Ensure that the correct row is edited by
229 	 * laying out one more time in a timerExec().
230 	 */
231 	if (tree != null) {
232 		Display display = tree.getDisplay();
233 		display.timerExec(-1, timer);
234 		display.timerExec(TIMEOUT, timer);
235 	}
236 }
237 
238 /**
239 * Sets the zero based index of the column of the cell being tracked by this editor.
240 *
241 * @param column the zero based index of the column of the cell being tracked by this editor
242 *
243 * @since 3.1
244 */
setColumn(int column)245 public void setColumn(int column) {
246 	int columnCount = tree.getColumnCount();
247 	// Separately handle the case where the tree has no TreeColumns.
248 	// In this situation, there is a single default column.
249 	if (columnCount == 0) {
250 		this.column = (column == 0) ? 0 : -1;
251 		resize();
252 		return;
253 	}
254 	if (this.column > -1 && this.column < columnCount){
255 		TreeColumn treeColumn = tree.getColumn(this.column);
256 		treeColumn.removeControlListener(columnListener);
257 		this.column = -1;
258 	}
259 
260 	if (column < 0  || column >= tree.getColumnCount()) return;
261 
262 	this.column = column;
263 	TreeColumn treeColumn = tree.getColumn(this.column);
264 	treeColumn.addControlListener(columnListener);
265 	resize();
266 }
267 
268 /**
269 * Specifies the <code>TreeItem</code> that is to be edited.
270 *
271 * @param item the item to be edited
272 */
setItem(TreeItem item)273 public void setItem (TreeItem item) {
274 	this.item = item;
275 	resize();
276 }
277 
278 /**
279 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above.
280 *
281 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
282 * specified in the TreeEditor constructor.
283 *
284 * @param editor the Control that is displayed above the cell being edited
285 * @param item the TreeItem for the row of the cell being tracked by this editor
286 * @param column the zero based index of the column of the cell being tracked by this editor
287 *
288 * @since 3.1
289 */
setEditor(Control editor, TreeItem item, int column)290 public void setEditor (Control editor, TreeItem item, int column) {
291 	setItem(item);
292 	setColumn(column);
293 	setEditor(editor);
294 }
295 @Override
setEditor(Control editor)296 public void setEditor (Control editor) {
297 	super.setEditor(editor);
298 	resize();
299 }
300 
301 /**
302 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above.
303 *
304 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
305 * specified in the TreeEditor constructor.
306 *
307 * @param editor the Control that is displayed above the cell being edited
308 * @param item the TreeItem for the row of the cell being tracked by this editor
309 */
setEditor(Control editor, TreeItem item)310 public void setEditor (Control editor, TreeItem item) {
311 	setItem(item);
312 	setEditor(editor);
313 }
314 
315 @Override
layout()316 public void layout () {
317 	if (tree == null || tree.isDisposed()) return;
318 	if (item == null || item.isDisposed()) return;
319 	int columnCount = tree.getColumnCount();
320 	if (columnCount == 0 && column != 0) return;
321 	if (columnCount > 0 && (column < 0 || column >= columnCount)) return;
322 	super.layout();
323 }
324 }
325